/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ws
 * Date: 2024-01-21
 * Time: 23:54
 *
 * https://www.nowcoder.com/share/jump/7745334641705852477107
 */
public class BM99 {
    public int[][] rotateMatrix(int[][] mat, int n) {
        // 特殊情况
        if (n <= 1) {
            return mat;
        }
        // 将原数组进行两次堆成交换
        // 1. 先按照 \ 对称交换
        for (int i = 0; i < n; i++) {
            for (int j = i; j < n; j++) {
                int tmp = mat[i][j];
                mat[i][j] = mat[j][i];
                mat[j][i] = tmp;
            }
        }
        // 2. 再按照 | 对称交换
        int col = n / 2;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < col; j++) {
                int tmp = mat[i][j];
                mat[i][j] = mat[i][n - j - 1];
                mat[i][n - j - 1] = tmp;
            }
        }
        // 返回结果
        return mat;
    }
}
